from rest_framework import serializers
from .models import Category, Tag, Link, UserVisit, UserPreference, Favorite
from core.mixins import BaseSerializerMixin

class CategorySerializer(BaseSerializerMixin):
    parent_name = serializers.ReadOnlyField(source='parent.name')
    children = serializers.SerializerMethodField()

    def get_children(self, obj):
        return CategorySerializer(obj.children.all(), many=True).data

    class Meta(BaseSerializerMixin.Meta):
        model = Category

class TagSerializer(BaseSerializerMixin):
    class Meta(BaseSerializerMixin.Meta):
        model = Tag

class LinkSerializer(BaseSerializerMixin):
    tags = TagSerializer(many=True, read_only=True)
    category_name = serializers.ReadOnlyField(source='category.name')
    visit_count = serializers.ReadOnlyField()
    is_favorited = serializers.SerializerMethodField()
    
    def get_is_favorited(self, obj):
        request = self.context.get('request')
        if request and request.user.is_authenticated:
            return Favorite.objects.filter(user=request.user, link=obj).exists()
        return False
    
    class Meta(BaseSerializerMixin.Meta):
        model = Link

class LinkTagsUpdateSerializer(serializers.Serializer):
    """链接标签更新序列化器"""
    tag_ids = serializers.ListField(
        child=serializers.IntegerField(),
        required=True,
        allow_empty=True,
        help_text='标签ID列表'
    )

class LinkTagsUpdateResponseSerializer(serializers.Serializer):
    """链接标签更新响应序列化器"""
    success = serializers.BooleanField(default=True)
    message = serializers.CharField(default='标签已更新')
    link = LinkSerializer()

class LinkVisitRequestSerializer(serializers.Serializer):
    """链接访问请求序列化器"""
    id = serializers.IntegerField(required=True, help_text='链接ID')

class LinkVisitResponseSerializer(serializers.Serializer):
    """链接访问响应序列化器"""
    success = serializers.BooleanField(default=True)
    message = serializers.CharField(default='访问记录已创建')

class LinkRecommendationResponseSerializer(serializers.Serializer):
    """推荐链接响应序列化器"""
    links = LinkSerializer(many=True)
    recommendation_reason = serializers.CharField(
        default="基于您的访问历史和偏好推荐",
        help_text="推荐原因说明"
    )

class UserVisitSerializer(BaseSerializerMixin):
    user_name = serializers.ReadOnlyField(source='user.username')
    link_title = serializers.ReadOnlyField(source='link.title')
    
    class Meta(BaseSerializerMixin.Meta):
        model = UserVisit

class UserPreferenceSerializer(BaseSerializerMixin):
    user_name = serializers.ReadOnlyField(source='user.username')
    favorite_categories = CategorySerializer(many=True, read_only=True)
    favorite_tags = TagSerializer(many=True, read_only=True)
    
    class Meta(BaseSerializerMixin.Meta):
        model = UserPreference

class FavoriteSerializer(BaseSerializerMixin):
    user_name = serializers.ReadOnlyField(source='user.username')
    link = LinkSerializer(read_only=True)
    
    class Meta(BaseSerializerMixin.Meta):
        model = Favorite
        fields = '__all__'

class FavoriteCreateSerializer(serializers.ModelSerializer):
    class Meta:
        model = Favorite
        fields = ['link']

class FavoriteResponseSerializer(serializers.Serializer):
    success = serializers.BooleanField(default=True)
    message = serializers.CharField()
    favorite = FavoriteSerializer()

class VisitStatsQuerySerializer(serializers.Serializer):
    """访问统计查询参数序列化器"""
    days = serializers.IntegerField(default=7, min_value=1, max_value=365)

class DailyStatsSerializer(serializers.Serializer):
    """每日统计序列化器"""
    created_at__date = serializers.DateField()
    visit_count = serializers.IntegerField()

class PopularLinkSerializer(serializers.Serializer):
    """热门链接序列化器"""
    id = serializers.IntegerField()
    title = serializers.CharField()
    url = serializers.URLField()
    visit_count = serializers.IntegerField()

class CategoryStatsSerializer(serializers.Serializer):
    """分类统计序列化器"""
    id = serializers.IntegerField()
    name = serializers.CharField()
    visit_count = serializers.IntegerField()

class HourlyStatsSerializer(serializers.Serializer):
    """小时统计序列化器"""
    hour = serializers.IntegerField()
    visit_count = serializers.IntegerField()

class WeekdayStatsSerializer(serializers.Serializer):
    """星期统计序列化器"""
    weekday = serializers.IntegerField()
    visit_count = serializers.IntegerField()

class TagStatsSerializer(serializers.Serializer):
    """标签统计序列化器"""
    id = serializers.IntegerField()
    name = serializers.CharField()
    visit_count = serializers.IntegerField()

class VisitStatsResponseSerializer(serializers.Serializer):
    """访问统计响应序列化器"""
    daily_stats = DailyStatsSerializer(many=True)
    hourly_stats = HourlyStatsSerializer(many=True)
    weekday_stats = WeekdayStatsSerializer(many=True)
    popular_links = PopularLinkSerializer(many=True)
    category_stats = CategoryStatsSerializer(many=True)
    tag_stats = TagStatsSerializer(many=True)
    total_visits = serializers.IntegerField()
